<?php

namespace app\admin\controller\point;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\lib\Tree;
use app\common\model\MallCate;
use app\common\model\MallGoods;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
/**
 * Class Pointgoods
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="积分商品管理")
 */
class Pointgoods extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model =new MallGoods();
        $this->assign('getStatusList', $this->model->getStatusList());
        $this->assign('getBargainAddressList', $this->model->getBargainAddressList());
        $this->assign('getPayTypeList', $this->model->getPayTypeList());
        $this->assign('getgoodstypeList',$this->model->getgoodstypeList());
    }

    /**
     * @NodeAnotation(title="积分商品列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where[]=[['g_type','in',[4,5]]];
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->field($this->field)
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [
                'cate_id|商品分类' => 'require',
                'title|商品名称' => 'require',
                'logo|商品logo' => 'require',
                'market_price|市场价格' => 'require',
                'cost_price|成本价格' => 'require',
                'price|销售价格' => 'require',
                'g_points|所需积分'=> 'require|integer',
                'total_stock|总库存'=> 'require|integer',
                'images|商品图片' => 'require',
                'describe|商品描述' => 'require',

            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        return $this->fetch();
    }

    /**
     * @return array|\int[][]|\string[][]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function getTreeCate(): array
    {
        $lists = (new MallCate())->field('id,pid,title')->where('status', 1)
            ->order('sort desc,id desc')
            ->select()
            ->toArray();
        return Tree::getInstance()->getPidMenuList(0, $lists);
    }

    /**
     * @NodeAnotation(title="选择商品分类")
     */
    public function getCateByList()
    {
        $treeList = $this->getTreeCate();
        $this->success('ok',$treeList);
    }
}